﻿using Basic.Oss.Implements;
using Basic.Oss.Interfaces;
using Basic.Oss.Options;
using Microsoft.Extensions.Configuration;

namespace Microsoft.Extensions.DependencyInjection;

/// <summary>
/// 添加OSS服务拓展
/// </summary>
public static class ServiceCollectionExtension
{
    /// <summary>
    /// 添加OSS服务
    /// </summary>
    /// <param name="services"></param>
    /// <param name="section"></param>
    /// <param name="lifetime"></param>
    /// <returns></returns>
    /// <exception cref="ArgumentNullException"></exception>
    public static IServiceCollection AddOssService(this IServiceCollection services, IConfigurationSection section, ServiceLifetime lifetime = ServiceLifetime.Singleton)
    {
        if (services == null) throw new ArgumentNullException(nameof(services));

        var option = section.Get<FileStorageOption>() ?? throw new ArgumentNullException(nameof(FileStorageOption));
        services.Configure<FileStorageOption>(section);

        if (option.StorageType == Basic.Oss.Const.EnumStorageType.AliYunOss)
        {
            if (option.AliOssConfig == null) throw new ArgumentNullException(nameof(FileStorageOption.AliOssConfig));

            services.Add(new ServiceDescriptor(typeof(IOssService), typeof(AliYunOssService), lifetime));
        }
        else if (option.StorageType == Basic.Oss.Const.EnumStorageType.LocalStorage)
        {
            if (option.LocalStorage == null) throw new ArgumentNullException(nameof(FileStorageOption.LocalStorage));

            services.Add(new ServiceDescriptor(typeof(IOssService), typeof(LocalDiskOssService), lifetime));
        }

        return services;
    }

}